<?php namespace Home\Controller;
use Hdphp\Controller\Controller;
//订单控制器
class OrderController extends Controller{
	//展示
	public function index(){
		//收货人地址
		$model = new \Common\Model\Address;
		$addrData = $model->get();
		//p($addrData);
		//分配数据
		View::with('addrData',$addrData);
		
		//载入模板
		View::make();
	}
	
	//异步添加收货人地址
	public function addAddress(){
		//接收数据
		$data = Q('post.');
		//p($data);
		//存入表里
		$addrname = Q('post.addrname');
		$phone_tel = Q('post.phone_tel');
		$phone_mob = Q('post.phone_mob');
		$region = '北京市';
		$email = Q('post.email');
		$address = Q('post.address');
		$user_uid = Q('post.userID');
		Db::table('address')->insert(['addrname'=>$addrname,'phone_tel'=>$phone_tel,'phone_mob'=>$phone_mob,'region'=>$region,'email'=>$email,'address'=>$address,'user_uid'=>$user_uid]);
		//返回数据
		echo json_encode($data);exit;  //前面不用p()，ajax才能成功返回数据
	}	
	
	//编辑地址
	public function edit(){
		//接收数据
		$data = Q('post.');
		//p($data);
		//存入表里
		$addrid = Q('post.addrid');
		$addrname = Q('post.addrname');
		$phone_tel = Q('post.phone_tel');
		$phone_mob = Q('post.phone_mob');
		$region = '北京市';
		$email = Q('post.email');
		$address = Q('post.address');
		$user_uid = Q('post.userID');
		Db::table('address')->where("addrid",$addrid)->update(['addrname'=>$addrname,'phone_tel'=>$phone_tel,'phone_mob'=>$phone_mob,'region'=>$region,'email'=>$email,'address'=>$address,'user_uid'=>$user_uid]);
		//成功提示
		View::success('编辑成功');		
	}
	//删除地址
	public function del(){
		//接收ID
		$addrid = Q('get.addrid',0,'intval');
		//1.删除对应的分类
		$model = new \Common\Model\Address;
	    $model->where("addrid=$addrid")->delete();
		View::success('删除成功');
	}
	//设为默认地址
	public function is_default(){
		//先设置所有的is_default = 0
		Db::table('address')->update(['is_default'=>0]);
		//接收ID
		$addrid = Q('get.addrid',0,'intval');
		Db::table('address')->where("addrid",$addrid)->update(['is_default'=>1]);
		//成功提示
		View::success('设置成功');
	}
} 
	
?>